using System;
using System.Collections.Generic;
using System.Text;

namespace TrueCode.Core.Diagnostics
{
	public class LogbookEntryTextFormatter
	{
		#region Fields
		private readonly String m_indent;
		#endregion

		#region .ctors
		public LogbookEntryTextFormatter( ) : this( 4 ) { }
		public LogbookEntryTextFormatter( Int32 indentSize )
		{
			var stringBuilder = new StringBuilder();
			for( var i = 0; i < indentSize; ++i )
			{
				stringBuilder.Append( " " );
			}
			m_indent = stringBuilder.ToString();
		}
		#endregion

		#region Entry points
		public String Format( Log log )
		{
			try
			{
				var stringBuilder = new StringBuilder( );

				for( var i = 0; i < log.Indent; ++i )
				{
					stringBuilder.Append( m_indent );
				}
				var indent = stringBuilder.ToString();
				stringBuilder.Append( log.Message );
				stringBuilder.Append( "   " );
				stringBuilder.Append( log.SenderInfo );


				stringBuilder.Append( "   " );
				stringBuilder.Append( String.Format( "Subsystem = {0}", log.Subsystem ) );

				stringBuilder.Append( "   " );
				stringBuilder.Append( "Category = " );
				foreach( String category in log.Categories )
				{
					stringBuilder.Append( String.Format( " {0},", category ) );
				}
				stringBuilder.AppendLine( );

				if( log.ExtendedInformation != null )
				{
					foreach( KeyValuePair< String, Object > pair in log.ExtendedInformation )
					{
						stringBuilder.Append( indent );
						stringBuilder.AppendFormat( "{0}: {1}", pair.Key, pair.Value );
						stringBuilder.AppendLine( );
					}
				}

				return stringBuilder.ToString( );
			}
			catch( Exception exc )
			{
				throw new Exception<LogbookEntryTextFormatter>( exc );
			}
		}
		#endregion
	}
}
